﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

/// <summary>
///PictureService 的摘要说明
/// </summary>
public class PictureService
{
    private static PictureService _instance = new PictureService();
    private IPictureDao _pictureDao = null;

    private PictureService()
	{
        _pictureDao = new PictureDao();
	}

    public static PictureService GetInstance()
    {
        return _instance;
    }

    public Picture GetPictureById(string pictureId)
    {
        Picture picture = null;

        picture = _pictureDao.GetPictureById(pictureId);

        return picture;
    }

    public IList<Picture> GetPictureList()
    {
        IList<Picture> pictureList = null;

        pictureList = _pictureDao.GetPictureList();

        return pictureList;
    }

    public IList<Picture> GetPictureListByCategory(string category)
    {
        IList<Picture> pictureList = null;

        pictureList = _pictureDao.GetPictureListByCategory(category);

        return pictureList;
    }

    public void DeletePicture(string pictureId)
    {
        Picture picture = _pictureDao.GetPictureById(pictureId);
        //删除硬盘上的图片
        _pictureDao.DelPicture(pictureId);
        try
        {
            string destFilePath = HttpContext.Current.Server.MapPath(@"~\pictures\") + picture.ImageName;
            string smallImgPath = HttpContext.Current.Server.MapPath(@"~\pictures\small") + picture.ImageName;
            System.IO.File.Delete(destFilePath);
            System.IO.File.Delete(smallImgPath);
        }
        catch (System.Exception e)
        {

        }

    }

    public void AddPicture(Picture picture)
    {
        try
        {
            picture.UploadDate = DateTime.Now;
            _pictureDao.AddPicture(picture);
            //保存图片
            string tmpdestFilePath = HttpContext.Current.Server.MapPath(@"~\pictures\tmp\") + picture.ImageName;
            string tmpsmallImgPath = HttpContext.Current.Server.MapPath(@"~\pictures\tmp\small") + picture.ImageName;
            string destFilePath = HttpContext.Current.Server.MapPath(@"~\pictures\") + picture.ImageName;
            string smallImgPath = HttpContext.Current.Server.MapPath(@"~\pictures\small") + picture.ImageName;
            System.IO.File.Copy(tmpdestFilePath, destFilePath);
            System.IO.File.Copy(tmpsmallImgPath, smallImgPath);

            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(HttpContext.Current.Server.MapPath(@"~\pictures\tmp\"));
            List<System.IO.FileInfo> filelist = new List<System.IO.FileInfo>(dir.GetFiles());
            foreach (System.IO.FileInfo i in filelist)
            {
                System.IO.File.Delete(i.FullName);
            }
        }
        catch (System.Exception e)
        {

        }
    }
}
